classdef junction < handle
    %JUNCTION class representing junctions
    properties
        roads;
        firstcar;
        capacity;
    end
    
    methods
        function enter(j, newNode)
           if isempty(j.firstCar)
              j.firstCar = newNode;
          else
              currentNode = j.firstCar;
              prevNode = j.firstCar;
              while ~isempty(currentNode)
                  prevNode = currentNode;
                  currentNode = currentNode.Next;                    
              end
              newNode.insertAfter(prevNode);
          end
        end
        
        function leave(j, road)
            leavingcar = j.firstcar; 
            j.firstcar = j.firstcar.Next;
            road.addCar(leavingcar);
        end
        
    end
end

